/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import java.util.ArrayList;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.help.HelpBroker;
import javax.help.HelpSet;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.plaf.FontUIResource;

import br.ufmg.dees.insane.commons.InsaneProperties;
import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.commons.view.CmdMenu;
import br.ufmg.dees.insane.commons.view.Interface;
import br.ufmg.dees.insane.commons.view.ModelList;
import br.ufmg.dees.insane.commons.view.SplashScreen;
import br.ufmg.dees.insane.ui.rich.full.command.AboutCommand;
import br.ufmg.dees.insane.ui.rich.full.command.BugReportCommand;
import br.ufmg.dees.insane.ui.rich.full.command.HelpTopicsCommand;
import br.ufmg.dees.insane.ui.rich.full.command.SetToEnglishCommand;
import br.ufmg.dees.insane.ui.rich.full.command.SetToPortugueseCommand;

public class InsaneInterface extends Interface{

	private static final long serialVersionUID = 1L;
	
	//Commands
    private Command englishCmd = new SetToEnglishCommand(this);
	private Command portugueseCmd = new SetToPortugueseCommand(this);
	private Command helpTopicsCmd = new HelpTopicsCommand(this);
    private Command bugReportCmd = new BugReportCommand(this);
	private Command aboutCmd = new AboutCommand(this);
	
	//Menu
	private JMenu menuSettings = new JMenu();
    private CmdMenu itemSettings = new CmdMenu(new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Settings16.gif")));
    private JMenu menuLanguages = new JMenu();
	private CmdMenu itemEnglish = new CmdMenu("English", new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/en.gif")));
	private CmdMenu itemPortuguese = new CmdMenu("Portugu\u00eas", new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/pt.gif")));
	
	private JMenu menuHelp = new JMenu();
	private CmdMenu itemHelpTopics = new CmdMenu(new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Help16.gif")));
	private CmdMenu itemBugReport = new CmdMenu(new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Bug16.gif")));
	private CmdMenu itemAbout = new CmdMenu(new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/About16.gif")));
	
	// Componentes JavaHelp
	private HelpSet helpset;
	private HelpBroker helpbroker;
	
	/**
	 * The constructor method. <br>
	 * Calls the init() method throwing an exception.
	 */
	public InsaneInterface(Locale locale) {
		Insane.advanceSplashProgress();
		currentLocale = locale;
		bundle = ResourceBundle.getBundle("br.ufmg.dees.insane.langPack.InsaneLangPack", currentLocale);
		try {
	    	this.setFonts();
	    	init();
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    SplashScreen.getInstance().finish();
	}
	
	/**
	 *  It's called by the constructor. <br>
	 *  Sets Panels, components and provides a window listener.
	 */
	private void init() throws Exception {
		
		addHelp("br/ufmg/dees/insane/help/postp/helpset.hs");
        linkHelp();
		
        this.setFonts();
		this.setTitle("INSANE " + this.getProperties().getVersion());
        this.setResizable(true);
        this.setSize(Toolkit.getDefaultToolkit().getScreenSize().width - 100, Toolkit.getDefaultToolkit().getScreenSize().height-100);
        this.setSize(new Dimension(800, 640));
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        
		this.getContentPane().setLayout(new BorderLayout());
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() {
		    public void windowClosing(WindowEvent e) {
		        System.exit(JFrame.EXIT_ON_CLOSE);
		    }
		} );
		
		Insane.advanceSplashProgress();
		
		Image ico = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Insane64.gif"));
		this.setIconImage(ico);
		
		this.setJMenuBar(menuBar);
		
		toolPanel.setLayout(new BoxLayout(toolPanel, BoxLayout.LINE_AXIS));
		this.getContentPane().add(toolPanel, BorderLayout.NORTH);
		this.getContentPane().add(desktop);
		
		
		//Setting Menus
		
		// Setting menu settings
        menuSettings.add(itemSettings);
//		itemSettings.setCommand(settingsCmd);
		itemSettings.addActionListener(this);
		
		itemEnglish.setCommand(englishCmd);
        itemEnglish.addActionListener(this);
		itemEnglish.setMnemonic(KeyEvent.VK_E);
		menuLanguages.add(itemEnglish);
		
		itemPortuguese.setCommand(portugueseCmd);
        itemPortuguese.addActionListener(this);
		itemEnglish.setMnemonic(KeyEvent.VK_P);
		menuLanguages.add(itemPortuguese);
		
		menuSettings.add(menuLanguages);
		
		//Setting menu help
        menuHelp.add(itemHelpTopics);
		itemHelpTopics.setCommand(helpTopicsCmd);
        itemHelpTopics.addActionListener(this);
        itemHelpTopics.setAccelerator(KeyStroke.getKeyStroke("F1"));
		
        menuHelp.addSeparator();
        
        menuHelp.add(itemBugReport);
        itemBugReport.setCommand(bugReportCmd);
		itemBugReport.addActionListener(this);
		
		menuHelp.add(itemAbout);
        itemAbout.setCommand(aboutCmd);
		itemAbout.addActionListener(this);
		
		this.setAllLabels();
		this.setInitialInterface();
		
	}
	
	public void setFonts() {
		try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            Font fontCurrent = UIManager.getFont("Currier");
            String name = fontCurrent.getName();
            
            //Creates similar font with the specified style and size
            FontUIResource fontResourceNew  = new FontUIResource(name, Font.PLAIN, 12);
            
            //Sets all items fonts
            UIManager.put("Button.font", fontResourceNew);
            UIManager.put("CheckBox.font", fontResourceNew);
            UIManager.put("CheckBoxMenuItem.font", fontResourceNew);
            UIManager.put("ColorChooser.font", fontResourceNew);
            UIManager.put("ComboBox.font", fontResourceNew);
            UIManager.put("DesktopIcon.font", fontResourceNew);
            UIManager.put("EditorPane.font", fontResourceNew);
            UIManager.put("InternalFrame.font", fontResourceNew);
            UIManager.put("Label.font", fontResourceNew);
            UIManager.put("List.font", fontResourceNew);
            UIManager.put("MenuBar.font", fontResourceNew);
            UIManager.put("Menu.font", fontResourceNew);
            UIManager.put("MenuItem.font", fontResourceNew);
            UIManager.put("OptionPane.font", fontResourceNew);
            UIManager.put("PasswordField.font", fontResourceNew);
            UIManager.put("PopupMenu.font", fontResourceNew);
            UIManager.put("ProgressBar.font", fontResourceNew);
            UIManager.put("RadioButton.font", fontResourceNew);
            UIManager.put("RadioButtonMenuItem.font", fontResourceNew);
            UIManager.put("ScrollPane.font", fontResourceNew);
            UIManager.put("TabbedPane.font", fontResourceNew);
            UIManager.put("TableHeader.font", fontResourceNew);
            UIManager.put("TextArea.font", fontResourceNew);
            UIManager.put("TextField.font", fontResourceNew);
            UIManager.put("TextPane.font", fontResourceNew);
            UIManager.put("TitledBorder.font", fontResourceNew);
            UIManager.put("ToggleButton.font", fontResourceNew);
            UIManager.put("ToolBar.font", fontResourceNew);
            UIManager.put("ToolTip.font", fontResourceNew);
            UIManager.put("Tree.font", fontResourceNew);
            UIManager.put("Viewport.font", fontResourceNew);
        }
        catch (Exception e){}
	}
	
	/** Returns the properties.
	*@return The properties.
	*/
	public InsaneProperties getProperties() {
		return properties;
	}
	
	/**
	 * The method sets the initial Interface
	 */
	public void setInitialInterface() {
		
		menuBar.removeAll();
		menuBar.add(menuSettings);
	    menuBar.add(menuHelp);
		
	    menuSettings.setEnabled(true);
	    menuHelp.setEnabled(true);

		this.getJMenuBar().updateUI();
		this.repaint();
	 
		desktop.removeAll();
		//desktop.setBackground(Color.GRAY);
		desktop.setBackground(Color.LIGHT_GRAY);
		desktop.repaint();
		desktop.updateUI();
		
		toolPanel.removeAll();
	    
		this.repaint();
	}
	
	/** Adds a Command to the undoList.
	 * @param cmd a the desired command
	 **/
	public void addToUndo(Command cmd) {
		
	}
	
	/** Adds a Command to the redoList.
	 * @param cmd a the desired command
	 */
	public void addToRedo(Command cmd) {

	}
	
	/** Disables the redo command.*/
	public void nothingToRedo() {
		
	}
	
	/** Disables the undo command.*/
	public void nothingToUndo() {
		
	}
	
	/**
	* Sets the Status Bar coord text.
	* @param x The first "X" coordinate.
	* @param y The second "Y" coordinate.
	*/
	public void setCoord(String x, String y) {
	}
	
	/**
	* Sets the Status Bar message.
	* @param str The text to be shown
	*/
	public void setMessage(String str) {
	}
	
	/**
	 * @return Returns the views.
	 */
	public ArrayList getViews() {
		return views;
	}

	/**
	 * @param views The views to set.
	 */
	public void setViews(ArrayList views) {
		this.views = views;
	}
	
	/**
	 * The method return the modelList.
	 * @return Returns The modelList.
	 */
	public ModelList getModelList() {
		return modelList;
	}
	
	/**
	 * The method set the field modelList from the parameter modelList.
	 * @param modelList The modelList to set.
	 */
	public void setModelList(ModelList modelList) {
		this.modelList = modelList;
	}

	/**
	 * The method return the itemHelp.
	 * @return Returns The itemHelp.
	 */
	public CmdMenu getItemHelp() {
		return itemHelpTopics;
	}
	
	/**
	 * The method set the field itemHelp from the parameter itemHelp.
	 * @param itemHelp The itemHelp to set.
	 */
	public void setItemHelp(CmdMenu itemHelp) {
		this.itemHelpTopics = itemHelp;
	}
	
	/**
	 * The method return the menuHelp.
	 * @return Returns The menuHelp.
	 */
	public JMenu getMenuHelp() {
		return menuHelp;
	}
	
	/**
	 * The method set the field menuHelp from the parameter menuHelp.
	 * @param menuHelp The menuHelp to set.
	 */
	public void setMenuHelp(JMenu menuHelp) {
		this.menuHelp = menuHelp;
	}
	
	public void addHelp(String nomeHelpSet) {
        // Finds the HelpSet file and creates HelpBroker
        ClassLoader loader = InsaneInterface.class.getClassLoader();
        try {
          URL url = HelpSet.findHelpSet(loader, nomeHelpSet);
          helpset = new HelpSet(loader, url);
          helpbroker = helpset.createHelpBroker();
        } catch (Exception e) {
          /* Treat */
        }
      }
      
      public void linkHelp() {
        // Associates the topic 'index' to clicking menu Help/Contents
        helpbroker.enableHelpOnButton(itemHelpTopics, "indiceINSANE", helpset);
        // Associates the topic 'topico1' to pressing key F1 in main panel
        helpbroker.enableHelpKey(this.getRootPane(), "indiceINSANE", helpset);
        
      }   
      
    /**
     *  Updates all labels to current locale ones.
  	 */
  	public void setAllLabels(){
  		
  		//Menu itens
  		menuSettings.setMnemonic(Integer.parseInt(bundle.getString("mneSettings")));
  		menuSettings.setText(bundle.getString("mSettings"));
  		itemSettings.setMnemonic(Integer.parseInt(bundle.getString("mneSettings")));
  		itemSettings.setText(bundle.getString("mSettings"));
  		menuLanguages.setMnemonic(Integer.parseInt(bundle.getString("mneLanguageS")));
  		menuLanguages.setText(bundle.getString("mLanguageS"));
  		
  		menuHelp.setMnemonic(Integer.parseInt(bundle.getString("mneHelp")));
  		menuHelp.setText(bundle.getString("mHelp"));
  		itemHelpTopics.setMnemonic(Integer.parseInt(bundle.getString("mneHelpTopics")));
  		itemHelpTopics.setText(bundle.getString("mHelpTopics"));
  		itemBugReport.setMnemonic(Integer.parseInt(bundle.getString("mneBugReport")));
  		itemBugReport.setText(bundle.getString("mBugReport"));
  		itemAbout.setMnemonic(Integer.parseInt(bundle.getString("mneAbout")));
  		itemAbout.setText(bundle.getString("mAbout"));
  	}
}
